package ui.activity.wallet.presenter;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.inject.Inject;

import base.BaseBiz;
import coom.Constants;
import ui.activity.wallet.biz.RewardsSummaryBiz;
import ui.activity.wallet.contract.RewardsSummaryContract;
import ui.model.RewardsDetailsBean;
import ui.model.RewardsListBean;

/**
 * Created by chenjian on 19/2/26
 */

public class RewardsSummaryPresenter implements RewardsSummaryContract.Presenter {

    RewardsSummaryContract.View view;
    RewardsSummaryBiz biz;
    List<RewardsDetailsBean> list = new ArrayList<>();
    private int year;
    private int month;
    private int day;
    private int pageIndex = 1;


    @Inject
    public RewardsSummaryPresenter(RewardsSummaryContract.View view) {
        this.view = view;
    }


    @Override
    public void setBiz(BaseBiz biz) {
        this.biz = (RewardsSummaryBiz) biz;
    }


    @Override
    public void getListInfo(final boolean isRefresh, int type) {
        if (isRefresh) {
            list.clear();
            pageIndex = 1;
        }
        view.loading(true);
        biz.getListInfo(type, pageIndex, Constants.PAGESIZE, new BaseBiz.Callback<RewardsListBean>() {
            @Override
            public void onSuccess(RewardsListBean info) {
                list.addAll(info.getRecords());
                view.upDateUI(list);
                pageIndex++;
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
                view.hasLoadMore(Constants.PAGESIZE == info.getRecords().size());
                view.loading(false);
            }

            @Override
            public void onFailure(RewardsListBean info) {
                view.loading(false);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
            }
        });
    }

    @Override
    public void getMonthInfo(boolean isRefresh, int type, long time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH) + 1;
        if (isRefresh) {
            list.clear();
            pageIndex = 1;
        }
        view.loading(true);
        biz.getMonthInfo(type, year, month, new BaseBiz.Callback<RewardsDetailsBean>() {
            @Override
            public void onSuccess(RewardsDetailsBean info) {
                list.add(info);
                view.upDateUI(list);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
                view.hasLoadMore(false);
                view.loading(false);
            }

            @Override
            public void onFailure(RewardsDetailsBean info) {
                view.loading(false);
                view.upDateUI(list);
                view.hasLoadMore(false);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
            }
        });
    }

}
